﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BOSS.Framework.Search.Result
{
    /// <summary>
    /// 结果字段模式定义类
    /// </summary>
    public class ResultFieldSchema
    {
        /// <summary>
        /// 高亮文字的默认长度
        /// </summary>
        public const int DEFAULT_LENGTH_FOR_HIGHLIGHTED = 200;

        private string m_name;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="name"></param>
        public ResultFieldSchema(string name)
        {
            this.m_name = name;
            this.FieldName = name;
            this.Highlighted = false;
            this.Length = 0;
            this.Ellipsis = null;
        }

        /// <summary>
        /// 获取 / 设置 映射名称
        /// </summary>
        public string Name
        {
            get
            {
                return this.m_name;
            }
            set
            {
                this.m_name = value;
                if (this.FieldName == null)
                {
                    this.FieldName = value;
                }
            }
        }

        /// <summary>
        /// 获取 / 设置 字段名称
        /// </summary>
        public string FieldName { get; set; }

        /// <summary>
        /// 获取 / 设置 是否高亮显示
        /// </summary>
        public bool Highlighted { get; set; }

        /// <summary>
        /// 获取 / 设置 字段最大长度
        /// </summary>
        public int Length { get; set; }

        /// <summary>
        /// 获取 / 设置 字段超长后，添加在尾部的缩略语
        /// </summary>
        public string Ellipsis { get; set; }

        /// <summary>
        /// 按照要求格式化查询字段（暂不支持高亮特性）
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public object Format(object value)
        {
            string strValue = Convert.ToString(value);
            if (strValue != null)
            {
                if (this.Length > 0)
                {
                    value = strValue.TrimTo(this.Length, (this.Ellipsis == null) ? string.Empty : this.Ellipsis);
                }
            }
            return value;
        }
    }
}
